<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="关联挂号人员表" prop="registerId">
        <el-select
          v-model="queryParams.registerId"
          placeholder="请选择关联挂号人员表"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发病日期" prop="time">
        <el-date-picker
          clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发病日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['treatment:consultationinformation:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['treatment:consultationinformation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['treatment:consultationinformation:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['treatment:consultationinformation:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="consultationinformationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="关联挂号人员表" align="center" prop="registerId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_user_sex"
            :value="scope.row.registerId"
          />
        </template>
      </el-table-column>
      <el-table-column label="发病日期" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="测量身高" align="center" prop="height" />
      <el-table-column label="测量体重" align="center" prop="weight" />
      <el-table-column label="体温" align="center" prop="temperature" />
      <el-table-column label="呼吸" align="center" prop="breath" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="血压" align="center" prop="bloodPressure" />
      <el-table-column label="患者描述" align="center" prop="patientTalk" />
      <el-table-column label="现病史历" align="center" prop="presentHistory" />
      <el-table-column label="既往史历" align="center" prop="pastHistory" />
      <el-table-column
        label="检查结果"
        align="center"
        prop="inspectionResult"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['treatment:consultationinformation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['treatment:consultationinformation:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联挂号人员表" prop="registerId">
          <el-select
            v-model="form.registerId"
            placeholder="请选择关联挂号人员表"
          >
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发病日期" prop="time">
          <el-date-picker
            clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发病日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="测量身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入测量身高" />
        </el-form-item>
        <el-form-item label="测量体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入测量体重" />
        </el-form-item>
        <el-form-item label="体温" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入体温" />
        </el-form-item>
        <el-form-item label="呼吸" prop="breath">
          <el-input v-model="form.breath" placeholder="请输入呼吸" />
        </el-form-item>
        <el-form-item label="心率" prop="heartRate">
          <el-input v-model="form.heartRate" placeholder="请输入心率" />
        </el-form-item>
        <el-form-item label="血压" prop="bloodPressure">
          <el-input v-model="form.bloodPressure" placeholder="请输入血压" />
        </el-form-item>
        <el-form-item label="患者描述" prop="patientTalk">
          <el-input v-model="form.patientTalk" placeholder="请输入患者描述" />
        </el-form-item>
        <el-form-item label="现病史历" prop="presentHistory">
          <el-input
            v-model="form.presentHistory"
            placeholder="请输入现病史历"
          />
        </el-form-item>
        <el-form-item label="既往史历" prop="pastHistory">
          <el-input v-model="form.pastHistory" placeholder="请输入既往史历" />
        </el-form-item>
        <el-form-item label="检查结果" prop="inspectionResult">
          <el-input
            v-model="form.inspectionResult"
            placeholder="请输入检查结果"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConsultationinformation,
  getConsultationinformation,
  delConsultationinformation,
  addConsultationinformation,
  updateConsultationinformation,
  getConsultationinformationByRegisterId,
} from "@/api/treatment/consultationinformation";

export default {
  name: "Consultationinformation",
  dicts: ["sys_user_sex"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查信息表格数据
      consultationinformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registerId: null,
        time: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // 检查信息人员对象
      Consultationinformation: {},
    };
  },
  created() {
    this.getList();
    // this.fetchConsultationInformation(this.queryParams.registerId);
    console.log(this.Consultationinformation);
  },
  methods: {
    /** 根据registerId 查询检查信息 */
    // fetchConsultationInformation(registerId) {
    //   getConsultationinformationByRegisterId(registerId)
    //     .then((response) => {
    //       this.Consultationinformation = response.data;
    //       console.log("Fetched Data:", this.Consultationinformation);
    //     })
    //     .catch((error) => {
    //       console.error("Error fetching consultation information:", error);
    //     });
    // },

    /** 查询检查信息列表 */
    getList() {
      this.loading = true;
      listConsultationinformation(this.queryParams).then((response) => {
        this.consultationinformationList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(response.rows);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registerId: null,
        time: null,
        height: null,
        weight: null,
        temperature: null,
        breath: null,
        heartRate: null,
        bloodPressure: null,
        patientTalk: null,
        presentHistory: null,
        pastHistory: null,
        inspectionResult: null,
        del: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检查信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConsultationinformation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检查信息";
      });
      getConsultationinformationByRegisterId(registerId).then((response) => {
        this.Consultationinformation = response.data;
        this.open = true;
        this.title = "检查信息对象";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateConsultationinformation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsultationinformation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除检查信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delConsultationinformation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "treatment/consultationinformation/export",
        {
          ...this.queryParams,
        },
        `consultationinformation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
